<%@ page import="org.json.simple.*" %> <%--json_simple_1-1.jar--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    import = "java.sql.*" %>

	<%
	request.setCharacterEncoding("UTF-8");
	
	String id_a = request.getParameter("id_a");
	String url = "jdbc:oracle:thin:@155.230.193.42:1521:oraknu";
	String user = "system";
	String pass = "DBLab012345";
	Connection con = null;
	String sql = null;
	int result;
	
	JSONObject resultJson = new JSONObject();
	
	try
	{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		System.out.println("success");
	}
	catch(ClassNotFoundException e)
	{
		System.err.println("error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con = DriverManager.getConnection(url, user, pass);
	}
	catch(SQLException e)
	{
		System.err.println("sql error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con.setAutoCommit(false);
		
		//get count
		sql = "select count(*) from LIKETABLE l where l.id_a = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, Integer.parseInt(id_a));
		ResultSet rs = pstmt.executeQuery();
		rs.next();
		int count = rs.getInt(1);
		resultJson.put("count",count);
			
		//get user liked true false
		String userId = (String)session.getAttribute("logID"); //user id
		sql = "select count(*) from LIKETABLE l where l.id_a = ? and l.id_u = ?";
		pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, Integer.parseInt(id_a));
		pstmt.setString(2, userId);
		rs = pstmt.executeQuery();
		rs.next();
		count = rs.getInt(1);
		boolean userLiked = count>=1;
		resultJson.put("like",userLiked);
		
		out.print(resultJson);
		
		pstmt.close();
		con.close();
	}
	catch(Exception e)
	{
		System.err.println("sql error = " + e.getMessage());
	}
			
%>